package com.balabala.gym_user.kafka_consumer;

import com.balabala.goods.constant.KafkaConstant;
import com.balabala.gym_user.constant.VipCardConstant;
import com.balabala.gym_user.pojo.entity.CourseVipCard;
import com.balabala.gym_user.service.CourseVipCardService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author 何涛
 * @Date: 2023/05/10/ 13:19
 * @description
 */
@Component
@Slf4j
public class KafkaConsumer {
    @Autowired
    CourseVipCardService courseVipCardService;
    @Autowired
    RedisTemplate redisTemplate;

    @KafkaListener(topics = KafkaConstant.COURSE_CARD_CLEAR)
    public void clearCourseCardCache(ConsumerRecord  record)
    {
        log.info("收到清空课程所支持会员卡的缓存的消息，topic:{},partition:{}",record.topic(),record.partition());
        log.info("课程id：{}",record.value());
        courseVipCardService.remove(new LambdaUpdateWrapper<CourseVipCard>().eq(CourseVipCard::getCourseId,record.value()));
        log.info("清空课程会员卡关系表中的数据完毕");
        log.info("开始清空缓存中的课程所支持的会员卡缓存。。。");
        redisTemplate.boundValueOps(VipCardConstant.CACHE_COURSE_SUPPORT_CARD+record.value()).expire(-1, TimeUnit.SECONDS);
        log.info("清除完毕。。。");
    }

}
